from gen import generate_test_data
from alg import Solution


def run_test():
    """生成测试数据并验证算法正确性"""
    # 生成测试数据
    nums = generate_test_data()
    print(f"测试用例: {nums[:10]}... (共{len(nums)}个元素)")

    # 计算结果
    solution = Solution()
    result = solution.maxSubArray(nums)

    # 打印结果
    print(f"计算结果: 最大子数组和为 {result}")

    # 简单验证（使用暴力算法）
    if len(nums) <= 1000:  # 避免大型数组的性能问题
        expected = max_sub_array_brute_force(nums)
        assert result == expected, f"验证失败! 期望结果: {expected}, 实际结果: {result}"
        print("验证通过: 结果与暴力算法一致")
    else:
        print("提示: 数组过大，跳过暴力验证")


def max_sub_array_brute_force(nums):
    """暴力解法，用于验证结果"""
    if not nums:
        return 0

    max_sum = nums[0]
    n = len(nums)

    for i in range(n):
        current_sum = 0
        for j in range(i, n):
            current_sum += nums[j]
            max_sum = max(max_sum, current_sum)

    return max_sum


if __name__ == "__main__":
    run_test()